Plots with ggplot2 are better plots!

Mireia Ramos (@mirthelle)

R-Ladies Barcelona, 27th of September 2017

About R-Ladies

What is R-Ladies?

Worldwide organization that promotes gender diversity in the R community via meetups and mentorship in a friendly and safe environment.

R-Ladies Barcelona

  • Created in November 2016 by Maëlle Salmon and Rebeca .
  • Season 2016-2017: 8 Meetups and more than 250 R-Ladies!

R-Ladies Barcelona v.2

  • Change of organizers summer 2017: Ania Alay and Mireia Ramos

More to come in this season 2017-2018!

Introduction

Who am I?

  • PhD student in Biomedicine and Bioinformatics. I analyze genomic and epigenomic data using R to increase knowledge in the pathological bases of Type 1 Diabetes.
  • #rcatladies

Why ggplot2?

ggplot2 is a data visualization package for R developed by Hadley Wickham that provides a structured approach to graphing.

Pros:

  • Standarized method for plotting.
  • Easy to create publication quality plot.
  • Allows creation of relatively complex plots with ease.
  • Most dominant statistics plotting package in R.

Why ggplot2?

Cons:

  • Might be a little bit difficult to understand at the begining.
  • Some times you might get lost between all the things you can change.

Why ggplot2?

But in the end it’s worth it!

Basics of ggplot2

Basic elements

We find 3 basic elements in a ggplot2 plot:

  • Data. The data you want to show in the plot.
  • Mapping. Maps a variable to an element in the plot (x and y axes, color, size, shape, etc.)
  • Geoms. Sets the type of graphic and elements that you want to plot.

Steps for plotting

  1. Convert your data.frame to long format (not always needed!).
  2. Use ggplot() with your data and map your x and y values.
  3. Add geoms that you want to show and map other aesthetical parameters.

Steps for plotting

1. Convert your data.frame to long format

Examples of tables in wide format (left) and long format (right)
Drug Response.CTRL Response.T1D
A 3 12
B 6 56
C 8 2
Drug Patient Response
A CTRL 3
B CTRL 6
C CTRL 8
A T1D 23
B T1D 56
C T1D 2

reshape2::melt() useful for converting from wide to long formats.

Steps for plotting

2. Map X and Y values

library(ggplot2)

ggplot(dat.long, aes(x=Drug, y=Response))

Steps for plotting

3. Add geoms and other aesthetics

library(ggplot2)

ggplot(dat.long, aes(x=Drug, y=Response)) +
  geom_point(aes(color=Patient), size=3)